// packageA/member/balance/transformLoveV/transformLoveV.js
var app = getApp();
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');
let isclick = true;
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    imgPath: app.globalData.imgPath,
    //爱心转账值
    sell_value: "",
    //爱心会员ID
    sell_id: "",
    // 转让手续费比率
    poundage: 0,
    trading_limit: 0,
    trading_fold: 0,
    love_name: "", //爱心值自定义名称
    usable: 0, // 登陆会员可用爱心值
    rate: 0,
    isTransformLoveV: false, //判断是否为转化爱心值
    balanceLang: "余额", //金额自定义
    // ycc资产转换冻结爱心值
    plugName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.name == 'transformLoveV') {
      this.setData({
        isTransformLoveV: true
      })
    }
    if(options.name == 'YccformFrozenLove'){
      this.setData({
        plugName:'YccformFrozenLove'
      })
    }
    this.getUsable();
    this.getLove();
    try {
      var value = wx.getStorageSync('balance');
      if (value) {
        this.setData({
          balanceLang: value
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    isclick = true;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  allConversion() {
    this.setData({
      sell_value: this.data.usable
    })
  },
  getUsable() {
    let that = this;
    let urlStr = "";
    let json = {};
    if(this.data.plugName == 'YccformFrozenLove'){
      urlStr = 'plugin.ycc-money.frontend.index.index'
    } else if(this.data.isTransformLoveV){
      urlStr = "plugin.love.Frontend.Controllers.page.index";
    } else {
      urlStr = "finance.balance.conver";
    }
    app._getNetWork({
      url: app.getNetAddresss(urlStr),
      data:json,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          if(this.data.plugName == 'YccformFrozenLove'){
            let basic_info = wx.getStorageSync("yz_basic_info");
            this.setData({
              froze_love_name : basic_info?.plugin_setting?.love?.froze_love_name || '冻结爱心值',
              usable : res.data.plugin_member.amount,
              rate : res.data.exchange_frozen_love_double,
              balanceLang:'余额'
            })
            wx.setNavigationBarTitle({
              title: `转换${this.data.froze_love_name}`,
            })
          } else if(this.data.isTransformLoveV){
            that.setData({
              love_name: res.data.love_name
            });
          } else{
            that.setData({
              usable: res.data.credit2,
              rate: res.data.rate,
              rate_1: Math.floor(((100 * res.data.rate) / 100) * 100) / 100,
              rate_2: Math.floor(((that.data.sell_value * res.data.rate) / 100) * 100) / 100
            });
          }
          
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: that.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getLove() {
    if(this.data.plugName == 'YccformFrozenLove') return
    try {
      var value = wx.getStorageSync('yz_basic_info');
      if (value) {
        let val = value.home;
        if (val.designer.love_name) {
          this.setData({
            love_name: val.designer.love_name
          });
        }
        wx.setNavigationBarTitle({
          title: '转化' + val.designer.love_name
        });

      }
    } catch (e) {
      // Do something when catch error
    }
    if (this.data.isTransformLoveV) {
      let that = this;
      let urlStr = app.getNetAddresss('plugin.love.Frontend.Modules.Trading.Controllers.trading.get-sell-love');
      app._getNetWork({
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              usable: res.data.love.usable,
              poundage: res.data.set.poundage,
              trading_limit: res.data.set.trading_limit,
              trading_fold: res.data.set.trading_fold
            });
            if (res.data.set.trading_fetter) {
              that.setDtaa({
                trading_fetter: res.data.set.trading_fetter
              });
            }
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  sellValueinp(e) {
    let val = e.detail.value;
    this.setData({
      sell_value: val,
      rate_2: Math.floor(((val * this.data.rate) / 100) * 100) / 100
    });
  },
  saleBalance() {
    let that = this;
    if (this.data.isTransformLoveV && !this.data.sell_value && this.data.plugName != 'YccformFrozenLove') {
      wx.showToast({
        icon: 'none',
        title: '请输入您要出售的数额',
        duration: 1500
      });
      return;
    }
    if (isclick) {
      isclick = false;
      let urlStr = "";
      let json = {};
      if(this.data.plugName == 'YccformFrozenLove'){
        urlStr = "plugin.ycc-money.frontend.exchange.exchange";
        json = {
          amount:this.data.sell_value
        }
      }else if(this.data.isTransformLoveV){
        urlStr = "plugin.love.Frontend.Modules.Trading.Controllers.trading.save-sell-love";
        json = {
          amount: this.data.sell_value
        }
      }else {
        urlStr = "finance.balance.convert-love-value";
        json = {
          convert_amount: this.data.sell_value
        }
      }
      app._postNetWork({
        url: app.getNetAddresss(urlStr),
        data: json,
        success: (resdata)=> {
          var res = resdata.data;
          if (res.result == 1) {
            setTimeout(function () {
              isclick = true;
            }, 1000);
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
            that.setData({
              sell_value: ''
            });
            if(this.data.isTransformLoveV && this.data.plugName != 'YccformFrozenLove'){
              that.getLove();
            }else{
              that.getUsable();
            }
          }else {
            isclick = true;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          setTimeout(function () {
            isclick = true;
          }, 1000);
          console.log(res);
        }
      });
    }

  }
});